/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.infrastructure;

import be.free.ts.support.requestfactory.GuiceRequestFactoryServlet;
import be.free.ts.support.requestfactory.RequestFactoryModule;
import be.free.ts.support.validation.ValidationModule;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.ServletModule;
import net.customware.gwt.dispatch.server.guice.GuiceStandardDispatchServlet;

import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

/**
 * @author Christophe De Blende
 * @since 08/12/12
 */
public class ApplicationModule extends ServletModule {

	@Override
	protected void configureServlets() {
		//persistence related modules
		install(new ValidationModule());
		install(new JpaPersistModule("timesheet-domain"));
		install(new RepositoryModule());
		install(new ServiceModule());

		//gwt related modules
		install(new RequestFactoryModule());
		install(new GwtActionHandlerModule());
		install(new GwtDispatchModule());

		//filters
		filter("/*").through(PersistFilter.class);

		//servlets
		Map<String, String> initParams = newHashMap();
		initParams.put("symbolMapsDirectory", "WEB-INF/classes/symbolMaps");
		serve("/gwtRequest").with(GuiceRequestFactoryServlet.class, initParams);
		serve("/Timesheet/dispatch").with(GuiceStandardDispatchServlet.class);
	}
}
